package com.one.common.manager.guide;

import android.view.View;

import androidx.fragment.app.Fragment;

import com.one.common.config.CPersisData;
import com.one.common.view.guide.NewbieGuide;
import com.one.common.view.guide.core.Builder;
import com.one.common.view.guide.core.Controller;
import com.one.common.view.guide.model.GuidePage;
import com.one.common.view.guide.model.HighlightOptions;
import com.one.common.view.guide.model.RelativeGuide;

/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2019/3/6 10:51
 * Description：
 **/
public class GuideManager {
    private static GuideManager instance;
    public static final String GUIDE_1 = "my_guide_1";
    public static final String GUIDE_2 = "my_guide_2";
    public static final String GUIDE_3 = "my_guide_3";

    public static final String GUIDE_4 = "my_guide_4";

    private GuideManager() {
    }

    public static GuideManager getInstance() {
        if (instance == null) {
            instance = new GuideManager();
        }
        return instance;
    }


    /**
     * 一个页面只有一页的引导
     */
    public static void guide(Fragment fragment, View view, GuideBean bean) {

        NewbieGuide.with(fragment)
                .setLabel(bean.getTag())
                .alwaysShow(true)
                .addGuidePage(getPageByBean(view, bean, false))
                .show();

        CPersisData.setIsGuide(false);
    }

    /**
     * 一个页面有两页的引导
     */
    public static void guide(Fragment fragment, View view, GuideBean bean, View view2, GuideBean bean2) {

        Builder builder = NewbieGuide.with(fragment)
                .setLabel(bean.getTag())
                .alwaysShow(true);

        if (view2 != null && bean2 != null) {
            builder.addGuidePage(getPageByBean(view, bean, true));
            builder.addGuidePage(getPageByBean(view2, bean2, false));
        }
        builder.show();
        CPersisData.setIsGuide(false);
    }

    /***
     * 车队长引导
     * @param fragment
     * @param view1
     * @param bean1
     * @param view2
     * @param bean2
     * @param view3
     * @param bean3
     */
    public static void guideCL(Fragment fragment,
                               View view1, GuideBean bean1,
                               View view2, GuideBean bean2,
                               View view3, GuideBean bean3) {

        Builder builder = NewbieGuide.with(fragment)
                .setLabel(bean1.getTag())
                .alwaysShow(true);

            builder.addGuidePage(getPageByBeanSimple(view1, bean1, true, 1));
            builder.addGuidePage(getPageByBeanSimple(view2, bean2, true, 2));
            builder.addGuidePage(getPageByBeanSimple(view3, bean3, false, 3));


        builder.show();
        CPersisData.setIsGuide(false);
    }


    /**
     * 创建一个引导页
     */
    private static GuidePage getPageByBean(View view, GuideBean bean, boolean isNextPage) {
        GuidePage guidePage = GuidePage.newInstance()
                .setEverywhereCancelable(false);//是否点击任意位置消失引导页，默认true;
        if (bean == null) return guidePage;
        if (bean.getLayoutRes1() == -1) return guidePage;

        HighlightOptions options1 = new HighlightOptions.Builder()
                .setRelativeGuide(new RelativeGuide(bean.getLayoutRes1(), bean.getGravity1()) {
                    @Override
                    protected void onLayoutInflated(View view, Controller controller) {
                        if (bean.getListener() != null && bean.getLayoutRes2() == -1) {
                            view.findViewById(bean.getClickRes()).setOnClickListener(v -> {
                                if (isNextPage) {
                                    controller.showPage(1);
                                }else {
                                    bean.getListener().onClick(v);
                                    controller.remove();
                                    if (!bean.isFinish()) {
                                        CPersisData.setIsGuide(true);
                                    } else {
                                        CPersisData.setIsGuide(false);
                                    }
                                }
                            });

                        }
                    }
                })
                .build();
        guidePage.addHighLightWithOptions(view, options1);
        if (bean.getLayoutRes2() != -1) {
            HighlightOptions options2 = new HighlightOptions.Builder()
                    .setRelativeGuide(new RelativeGuide(bean.getLayoutRes2(), bean.getGravity2()) {
                        @Override
                        protected void onLayoutInflated(View view, Controller controller) {
                            if (bean.getListener() != null) {
                                view.findViewById(bean.getClickRes()).setOnClickListener(v -> {
                                    if (isNextPage) {
                                        controller.showPage(1);
                                    }else {
                                        bean.getListener().onClick(v);
                                        controller.remove();
                                        if (!bean.isFinish()) {
                                            CPersisData.setIsGuide(true);
                                        } else {
                                            CPersisData.setIsGuide(false);
                                        }
                                    }
                                });
                            }
                        }
                    })
                    .build();
            guidePage.addHighLightWithOptions(view, options2);
        }
        return guidePage;
    }



    private static GuidePage getPageByBeanSimple(View view, GuideBean bean, boolean isNextPage, int index) {
        GuidePage guidePage = GuidePage.newInstance().setEverywhereCancelable(false);
        if (bean == null) return guidePage;
        if (bean.getLayoutRes1() == -1) return guidePage;

        HighlightOptions options1 = new HighlightOptions.Builder()
                .setRelativeGuide(new RelativeGuide(bean.getLayoutRes1(), bean.getGravity1()) {
                    @Override
                    protected void onLayoutInflated(View view, Controller controller) {
                        if (bean.getListener() != null) {
                            view.findViewById(bean.getClickRes()).setOnClickListener(v -> {
                                if (isNextPage) {
                                    controller.showPage(index);
                                }else {
                                    bean.getListener().onClick(v);
                                    controller.remove();
                                    if (!bean.isFinish()) {
                                        CPersisData.setIsGuide(true);
                                    } else {
                                        CPersisData.setIsGuide(false);
                                    }
                                }
                            });

                        }
                    }
                })
                .build();
        guidePage.addHighLightWithOptions(view, options1);
        return guidePage;
    }

}
